package com.yangrong.newspublicservice.service.impl;

import com.yangrong.newspublicservice.dao.TbHomepageMenuDao;
import com.yangrong.newspublicservice.entity.TbHomepageMenuEntity;
import com.yangrong.newspublicservice.service.TbHomepageMenuService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yangrong.common.utils.PageUtils;
import com.yangrong.common.utils.Query;


@Service("tbHomepageMenuService")
public class TbHomepageMenuServiceImpl extends ServiceImpl<TbHomepageMenuDao, TbHomepageMenuEntity> implements TbHomepageMenuService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TbHomepageMenuEntity> page = this.page(
                new Query<TbHomepageMenuEntity>().getPage(params),
                new QueryWrapper<TbHomepageMenuEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<TbHomepageMenuEntity> getMenuList(Map<String, Object> params) {
        List<TbHomepageMenuEntity> list = new ArrayList<>();
        params.put("parent_id", null);
        list = baseMapper.selectByMap(params);
        return this.getChildMenu(list);
    }

    private List<TbHomepageMenuEntity> getChildMenu(List<TbHomepageMenuEntity> parent) {
        parent.stream().forEach(item -> {
            if (item.getHasChildMenu() == 1) {
                Map<String, Object> params = new HashMap<>();
                params.put("parent_id", item.getId());
                List<TbHomepageMenuEntity> childList = baseMapper.selectByMap(params);
                getChildMenu(childList);
                item.setChildList(childList);
            }
        });

        return parent;
    }

}